package cas.ihep.spark.rdd;

import cas.ihep.spark.util.ManagedResource;

import java.io.Closeable;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

class DefaultManagedResource<T> implements ManagedResource<T> ,Closeable{

    T resource;
    private Set<Closeable> closer;
    DefaultManagedResource(T t){
        resource=t;
        closer=null;
    }
    public T get(){
        return resource;
    }
    public void finalizeResource(Closeable c){
        if(closer==null){
            closer=new HashSet<>();
        }
        closer.add(c);
    }
    public void close()throws IOException {
        if(closer!=null) {
            for (Closeable c : closer) {
                c.close();
            }
        }
    }
}
